package com.java.system.controller;

import cn.hutool.core.util.StrUtil;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.system.entity.Resource;
import com.java.system.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 带条件分页查询资源
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = resourceService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 根据主键查询资源
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        Resource resource = resourceService.getById(id);

        return R.ok(resource);
    }

    /**
     * 新增资源
     */
    @PostMapping("/save")
    public R save(@RequestBody String jsonStr){
        if (StrUtil.isBlank(jsonStr)) {
            return R.error("参数错误");
        }
        resourceService.saveJson(jsonStr);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        resourceService.removeById(id);
        return R.ok();
    }

    /**
     * 资源树查询
     */
    @GetMapping("/load-tree")
    public R loadTree() {
        List<Resource> resources = resourceService.loadTree();
        return R.ok(resources);
    }
}
